package com.tengju.user.infrastructure.repository.club;

import com.tengju.user.domain.model.club.Club;
import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.model.club.ClubRepository;
import com.tengju.user.domain.shared.BasePage;
import com.tengju.user.domain.shared.IdObject;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ClubMetaDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.ClubMetaDOCustomMapper;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.ClubMetaDOMapper;
import com.tengju.user.infrastructure.repository.club.convert.ClubConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/1/13
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ClubMetaRepositoryImpl implements ClubRepository {

    private final ClubMetaDOMapper clubMetaDOMapper;
    private final ClubMetaDOCustomMapper clubMetaDOCustomMapper;

    @Override
    public Club findById(ClubId clubId) {
        if (clubId == null) {
            return null;
        }
        ClubMetaDO clubMetaDO = clubMetaDOMapper.selectByPrimaryKey(clubId.getValue());
        return ClubConvert.clubMetaToModel(clubMetaDO);
    }

    @Override
    public List<Club> findByIds(List<ClubId> clubIds) {
        if (CollectionUtils.isEmpty(clubIds)) {
            return Collections.emptyList();
        }

        List<Long> clubIdsValue = clubIds.stream().map(IdObject::getValue).collect(Collectors.toList());

        List<ClubMetaDO> clubMetaDOList = clubMetaDOCustomMapper.queryByIds(clubIdsValue);
        return clubMetaDOList.stream().map(ClubConvert::clubMetaToModel).collect(Collectors.toList());
    }

    @Override
    public void update(Club club) {
        if (club == null) {
            return ;
        }
        ClubMetaDO clubMetaDO =  ClubConvert.metaToDO(club);
        clubMetaDOMapper.updateByPrimaryKeySelective(clubMetaDO);
    }

    @Override
    public Club findByName(String clubName) {
        ClubMetaDO clubMetaDO = clubMetaDOCustomMapper.findByName(clubName.trim());
        return ClubConvert.clubMetaToModel(clubMetaDO);
    }

    @Override
    public ClubId save(Club club) {
        ClubMetaDO clubMetaDO =  ClubConvert.metaToDO(club);
        int clubId = clubMetaDOMapper.insertSelective(clubMetaDO);
        return new ClubId(clubMetaDO.getId());
    }

    @Override
    public BasePage<Club> query(Long clubId, String clubName, Long founderIdCode, Integer offset, Integer pageSize) {
        BasePage<Club> result = new BasePage();
        int count = clubMetaDOCustomMapper.count(clubId, clubName, founderIdCode);
        result.setTotal(count);
        List<ClubMetaDO> clubMetaDOS = clubMetaDOCustomMapper.query(clubId, clubName, founderIdCode,offset,pageSize);
        List<Club> clubs = clubMetaDOS.stream().map(ClubConvert::clubMetaToModel).collect(Collectors.toList());
        result.setDatas(clubs);
        return result;
    }

    @Override
    public List<Club> queryAll() {
        List<ClubMetaDO> clubMetaDOList = clubMetaDOCustomMapper.queryAll();
        return clubMetaDOList.stream().map(ClubConvert::clubMetaToModel).collect(Collectors.toList());
    }

}
